package com.event.starter.config;

import com.event.common.annotation.RepoNameSpace;
import com.event.common.face.IEventHandle;
import com.event.core.context.AnnotationContext;
import com.event.core.context.EventContext;
import com.event.starter.interceptor.MybatisUpdateInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import javax.annotation.Resource;

/**
 * AutoMybatisConfig，根据开关自动加载配置
 */
@Configuration
@ComponentScan(basePackages = {"com.event"})
@ConditionalOnProperty(name = "config.mybatis.enable", havingValue = "true")
public class AutoMybatisConfig {
    // 注解上下文
    @Resource
    private AnnotationContext annotationContext;
    // 事件处理器
    @Resource
    private IEventHandle eventHandle;

    @Bean
    @ConditionalOnMissingBean(EventContext.class)
    public EventContext eventContext(){
        return new EventContext(eventHandle);
    }

    @Bean
    @DependsOn("annotationsEventProcessor")
    public MybatisUpdateInterceptor getMybatisPlusUpdateInterceptor(EventContext eventContext){
        return new MybatisUpdateInterceptor(annotationContext,eventContext,predicate -> predicate.getAnnotation().annotationType().equals(RepoNameSpace.class));
    }

}
